package org.LZQ.ShortLink.admin.remote;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.LZQ.ShortLink.admin.common.convention.result.Result;
import org.LZQ.ShortLink.admin.dto.req.RecycleBinRecoverReqDTO;
import org.LZQ.ShortLink.admin.dto.req.RecycleBinRemoveReqDTO;
import org.LZQ.ShortLink.admin.remote.dto.req.ShortLinkStatsAccessRecordReqDTO;
import org.LZQ.ShortLink.admin.remote.dto.resp.*;
import org.LZQ.ShortLink.admin.remote.dto.req.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用服务
 */
public interface ShortLinkActualRemoteService {
    /**
     * 短链接创建
     * @param requestParam
     * @return
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam){
        String resultBody = HttpUtil.post("http://localhost:8082/api/shortLink/project/shortLink/create", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBody, new TypeReference<>() {
        });
    }
    /**
     * 批量创建短链接
     * @param requestParam
     * @return
     */
    default Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(ShortLinkBatchCreateReqDTO requestParam){
        String resultBody = HttpUtil.post("http://localhost:8082/api/shortLink/project/shortLink/create/batch",
                JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBody, new TypeReference<>() {});
    }
    /**
     * 修改短连接
     * @param requestParam
     */

    default void updateShortLink(ShortLinkUpdateReqDTO requestParam){
        HttpUtil.post("http://localhost:8082/api/shortLink/project/shortLink/update", JSON.toJSONString(requestParam));
    }

    /**
     * 分页查询短连接
     * @param requestParam
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://localhost:8082/api/shortLink/project/shortLink/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    /**
     * 查询短链接分组总量
     * @param requestParam
     * @return
     */
    default Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(List<String> requestParam) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("requestParam",requestParam);
        String resultPageStr = HttpUtil.get("http://localhost:8082/api/shortLink/project/shortLink/count", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }



    default void restoreUrl(String shortUri, ServletRequest request, ServletResponse response) throws IOException {
        if (shortUri.equals("favicon.ico")){
            return;
        }
        String url = "http://localhost:8082/" + shortUri;
        HttpUtil.get(url);
    }


    default Result<String> getTitleByUrl(@RequestParam("url") String url) {
        String resultStr = HttpUtil.get("http://localhost:8082/api/shortLink/project/title?url=" + url);
        return JSON.parseObject(resultStr, new TypeReference<>() {
        });
    }

    /**
     * 保存回收站
     * @param requestParam
     */
    default void saveRecycleBin(RecycleBinSaveReqDTO requestParam){
         HttpUtil.post("http://localhost:8082/api/shortLink/project/recycleBin/save"
                , JSON.toJSONString(requestParam));
    }

    /**
     * 分页查询回收站短连接
     * @param requestParam
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gidList",requestParam.getGidList());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://localhost:8082/api/shortLink/project/recycleBin/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    /**
     * 恢复短链接
     * @param requestParam
     */
    default void recoverRecycleBin(RecycleBinRecoverReqDTO requestParam){
        HttpUtil.post("http://localhost:8082/api/shortLink/project/recycleBin/recover"
                , JSON.toJSONString(requestParam));
    }

    /**
     * 删除短链接
     * @param requestParam
     */
    default void removeRecycleBin(RecycleBinRemoveReqDTO requestParam){
        HttpUtil.post("http://localhost:8082/api/shortLink/project/recycleBin/remove"
                , JSON.toJSONString(requestParam));
    }

    /**
     * 短链接访问统计
     * @param requestParam
     * @return
     */
    default Result<ShortLinkStatsRespDTO> shortLinkStats(ShortLinkStatsReqDTO requestParam){
        String resultBody =
                HttpUtil.get("http://localhost:8082/api/shortLink/project/stats",
                        BeanUtil.beanToMap(requestParam));
        return JSON.parseObject(resultBody, new TypeReference<>() {
        });
    }

    /**
     * 访问单个短链接指定时间内访问记录监控数据
     * @param requestParam
     * @return
     */
    default Result<IPage<ShortLinkStatsAccessRecordRespDTO>> shortLinkStatsAccessRecord(ShortLinkStatsAccessRecordReqDTO requestParam){

        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(requestParam,false,true);
        stringObjectMap.remove("orders");
        stringObjectMap.remove("records");
        String resultPageStr = HttpUtil.get("http://localhost:8082/api/shortLink/project/stats/access-record", stringObjectMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }
    default Result<IPage<ShortLinkStatsAccessRecordRespDTO>> groupShortLinkStatsAccessRecord(ShortLinkGroupStatsAccessRecordReqDTO requestParam){
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(requestParam,false,true);
        stringObjectMap.remove("orders");
        stringObjectMap.remove("records");
        String resultPageStr = HttpUtil.get("http://localhost:8082/api/shortLink/project/stats/access-record/group", stringObjectMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    default Result<ShortLinkStatsRespDTO> groupShortLinkStats(ShortLinkGroupStatsReqDTO requestParam){
        String resultBody =
                HttpUtil.get("http://localhost:8082/api/shortLink/project/stats/group",
                        BeanUtil.beanToMap(requestParam));
        return JSON.parseObject(resultBody, new TypeReference<>() {
        });
    }


}
